
function numIslands(grid: string[][]): number {
    let results: number = 0;
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[i].length; j++) {
            if (grid[i][j] === '1') {
                results++;
                dfs(grid, i, j);
            }
        }
    }
    return results;
};

function dfs(grid: string[][], i: number, j: number) {

    if (i === -1 || i === grid.length || grid[i][j] !== '1') {
        return
    }
    grid[i][j] = '0'
    dfs(grid, i + 1, j)
    dfs(grid, i - 1, j)
    dfs(grid, i, j + 1)
    dfs(grid, i, j - 1)
}